"use strict";
$(function () {
    $("#orgEmployeeGrid").Grid({
        url: '../org/employee/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '公司', name: 'companyName', index: 'company_name', width: 80},
			{label: '公司', name: 'companyId', index: 'company_id', hidden: true},
			{label: '部门', name: 'departName', index: 'depart_id', width: 80},
			{label: '职位', name: 'positionName', index: 'position_id', width: 80},
			{label: '名称', name: 'name', index: 'name', width: 80},
			{label: '手机号', name: 'phone', index: 'phone', width: 80},
			{label: '生日', name: 'birthday', index: 'birthday', width: 80,
				formatter:function(cellvalue, options, rowObject){
					if(cellvalue){
						return moment(cellvalue).format('YYYY-MM-DD');
					}else{
						return "";
					}
				}
			}]
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el: '#orgEmployee',
	data: {
        showList: true,
        title: null,
		orgEmployee: {},
		departmentList: [],
		positionList: [],
		ruleValidate: {
			name: [
				{required: true, message: '姓名不能为空', trigger: 'blur'}
			],
			companyId: [
		        {required: true, message: '公司不能为空', trigger: 'blur'}
		    ],
//		    positionId: [
//		        {required: true, message: '职位不能为空', trigger: 'blur'}
//		    ],
		    phone: [
 		        {required: true, message: '手机号不能为空', trigger: 'blur'}
 		    ]
		},
		q: {
			companyName: '',name: '',phone: ''
		}
	},
	methods: {
		changeDepart:function(data){
			for (var i = 0; i < vm.positionList.length; i++) {
				if(data==vm.positionList[i].departmentId){
					vm.positionList[i].isShow=1;
				}else{
					vm.positionList[i].isShow=0;
				}
			}
		},
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: "../org/company/select",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#orgCompanyTree"), setting, r.deptList);
                    var node = ztree.getNodeByParam("deptId", vm.orgEmployee.companyId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.orgEmployee.companyName = node.name;
                    } else {
                        node = ztree.getNodeByParam("deptId", '01');
                        ztree.selectNode(node);
                        vm.orgEmployee.companyName = node.name;
                    }
                }
            });
        },
        getDepartment: function () {
        	//加载部门树
        	Ajax.request({
        		url: "../org/department/queryAll?companyId="+vm.orgEmployee.companyId,
        		async: true,
        		successCallback: function (r) {
        			r.list.push({id:'companyId',name:'本公司'});
        			vm.departmentList=r.list;
        		}
        	});
        },
        getPosition: function () {
        	//加载部门树
        	Ajax.request({
        		url: "../org/position/queryAll?companyId="+vm.orgEmployee.companyId,
        		async: true,
        		successCallback: function (r) {
        			for (var i = 0; i < r.list.length; i++) {
        				if(r.list[i].departmentId){
        					if(vm.orgEmployee.departId){
        						r.list[i].isShow=1;
        					}else{
        						r.list[i].isShow=0;
        					}
        				}else{
        					if(r.list[i].departmentId){
        						r.list[i].isShow=0;
        					}else{
        						r.list[i].isShow=1;
        					}
        					r.list[i].departmentId="companyId";
        					r.list[i].name+="~公司";
        				}
					}
        			vm.positionList=r.list;
        		}
        	});
        },
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.orgEmployee = {};
			vm.getDept();
		},
		update: function (event) {
            var id = getSelectedRow("#orgEmployeeGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.orgEmployee.id == null ? "../org/employee/save" : "../org/employee/update";
            vm.orgEmployee.birthday = $('#birthday input').val();
            if(vm.orgEmployee.birthday!=null&&vm.orgEmployee.birthday!=''){
                vm.orgEmployee.birthday = moment(vm.orgEmployee.birthday).format('YYYY-MM-DD HH:mm:ss');
            }
            if("companyId"==vm.orgEmployee.departId){
            	vm.orgEmployee.departId=null;
            }
            
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.orgEmployee),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		deptTree: function () {
            openWindow({
                title: "选择上级",
                area: ['300px', '450px'],
                content: jQuery("#orgCompanyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.orgEmployee.companyId = node[0].deptId;
                    vm.orgEmployee.companyName = node[0].name;
                    //设置公司下所有的部门
                    vm.getDepartment();
                    vm.getPosition();
                    layer.close(index);
                }
            });
        },
		del: function (event) {
            var ids = getSelectedRows("#orgEmployeeGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../org/employee/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../org/employee/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.orgEmployee = r.orgEmployee;
                    vm.getDepartment();
                    vm.getPosition();
                    vm.getDept();
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#orgEmployeeGrid").jqGrid('getGridParam', 'page');
			$("#orgEmployeeGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'companyName': vm.q.companyName,'phone': vm.q.phone},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {companyName: '',name: '',phone: ''};
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});